// 商品流水相关API
// import request from '@/utils/request.js'

// 简单的请求函数，替代request工具
const request = (config) => {
	return new Promise((resolve) => {
		// 模拟API请求
		setTimeout(() => {
			resolve({ code: 200, data: null, message: 'success' });
		}, 500);
	});
}

// 获取商品流水列表
export function getProductFlowList(params) {
	return request({
		url: '/product/flow/list',
		method: 'GET',
		data: params
	})
}

// 获取商品流水详情
export function getProductFlowDetail(id) {
	return request({
		url: `/product/flow/detail/${id}`,
		method: 'GET'
	})
}

// 导出商品流水数据
export function exportProductFlow(params) {
	return request({
		url: '/product/flow/export',
		method: 'POST',
		data: params
	})
}

// 模拟数据 - 获取商品流水列表
export function getMockProductFlowList(params = {}) {
	return new Promise((resolve) => {
		setTimeout(() => {
			const { page = 1, pageSize = 20, keyword = '', dateType = 'day', selectedDate = '' } = params
			
			// 生成模拟数据
			const mockData = []
			const productNames = [
				'某某商品', '优质商品', '热销商品', '新品商品', '特价商品',
				'精选商品', '推荐商品', '畅销商品', '限量商品', '经典商品'
			]
			
			const flowTypes = [
				{ type: '采购', color: '#19be6b' },
				{ type: '退货', color: '#fa3534' }
			]
			
			// 生成最近30天的流水记录
			for (let i = 1; i <= 100; i++) {
				const randomDays = Math.floor(Math.random() * 30)
				const date = new Date()
				date.setDate(date.getDate() - randomDays)
				
				const flowType = flowTypes[Math.floor(Math.random() * flowTypes.length)]
				const quantity = flowType.type === '采购' ? 
					Math.floor(Math.random() * 1000) + 100 : 
					-(Math.floor(Math.random() * 300) + 50)
				
				const item = {
					id: i,
					date: date.toLocaleDateString('zh-CN', { month: '2-digit', day: '2-digit' }),
					fullDate: date.toLocaleDateString('zh-CN'),
					type: flowType.type,
					typeColor: flowType.color,
					productName: productNames[Math.floor(Math.random() * productNames.length)],
					productCode: `SP${String(i).padStart(4, '0')}`,
					quantity: quantity,
					quantityText: quantity > 0 ? `+${quantity}` : `${quantity}`,
					remark: flowType.type === '采购' ? '正常采购' : '质量问题退货',
					createTime: date.toISOString(),
					timestamp: date.getTime()
				}
				
				// 应用搜索筛选
				if (keyword) {
					const searchKeyword = keyword.toLowerCase()
					if (!item.productName.toLowerCase().includes(searchKeyword) && 
						!item.productCode.toLowerCase().includes(searchKeyword)) {
						continue
					}
				}
				
				// 应用日期筛选
				if (selectedDate) {
					const selected = new Date(selectedDate)
					if (dateType === 'day') {
						// 按日筛选
						if (item.fullDate !== selected.toLocaleDateString('zh-CN')) {
							continue
						}
					} else if (dateType === 'month') {
						// 按月筛选
						if (date.getFullYear() !== selected.getFullYear() || 
							date.getMonth() !== selected.getMonth()) {
							continue
						}
					}
				}
				
				mockData.push(item)
			}
			
			// 按时间倒序排列
			mockData.sort((a, b) => b.timestamp - a.timestamp)
			
			// 分页处理
			const startIndex = (page - 1) * pageSize
			const endIndex = startIndex + pageSize
			const list = mockData.slice(startIndex, endIndex)
			
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					list: list,
					total: mockData.length,
					page: page,
					pageSize: pageSize,
					hasMore: endIndex < mockData.length
				}
			})
		}, 500)
	})
}

// 模拟数据 - 获取商品流水统计
export function getMockProductFlowStats(params = {}) {
	return new Promise((resolve) => {
		setTimeout(() => {
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					totalRecords: 100,
					totalPurchase: 45000,
					totalReturn: -3500,
					netFlow: 41500,
					todayRecords: 8,
					monthRecords: 65,
					purchaseRecords: 70,
					returnRecords: 30
				}
			})
		}, 400)
	})
}
